// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //查找文本
    searchinfo : '',
    //搜索框颜色
    color:'',
    isSelect:false,
    select:true,
    type:'',
    typec:'',
    choose:false,
    up:'/pic/三角上黑.png',
    down:'/pic/三角下黑.png',
    choosevalue:0,
    splitarray:[
      {index:0,type:'默认排序',display:true},
      {index:1,type:'最新',display:false},
      {index:2,type:'最热',display:false}],
    itemarray:[],
      personaltarget:'none',
      wxAva:"",
      userName:"",
      academy:"",
      userClass:"",
      contactPhone:"",
      contactWx:"",
      contactQq:""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options)
    var that = this
    
    var searchtext = options.searchinfo;

    if(searchtext != ""){
      //展示搜索结果
      wx.request({
        url: 'http://127.0.0.1:8080/item/showSelectResult',
        method:"POST",
        data:{searchinfo:options.searchinfo},
        success(res){
          if(res.data.length != 0){
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic  = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              itemarray:res.data
            })
          }
          // console.log(res)
        }

      })
    }
    this.setData({
      searchinfo:options.searchinfo,
      color:"blue",
      typec:this.data.splitarray[0].type  
    })
    //request发送后台搜索物品

  },

  /*搜索框*/
  //获取搜索信息
  searchinfo:function(e){
    
    var text = e.detail.value;
    this.setData({
      searchinfo : text
    })
    

  },
  //输入改变边框颜色
  inputchange:function() {
      this.setData({
        color:"blue"
      })
    },
  //不输入退回边框颜色
  inputreturn:function() {
      this.setData({
        color:""
      })
  },
  //搜索功能
  search:function(e) {
    var that = this;
    var info = this.data.searchinfo;
    if(info == ''){
      /*搜索为空提示 */
      wx.showToast({
        title: '搜索内容为空',
        icon:"error",
        duration:1000
      })
    }else{
        //展示搜索结果
        wx.request({
          url: 'http://127.0.0.1:8080/item/showSelectResult',
          method:"POST",
          data:{searchinfo:info},
          success(res){
            if(res.data.length != 0){
              for(var i=0;i<res.data.length;i++){
                res.data[i].itemPic  = JSON.parse(res.data[i].itemPic)
              }
              that.setData({
                itemarray:res.data
              })
            }
            console.log(res)
          }
  
        })
      
    }
  },

  /*改变种类 */
  changetype(e){
    var value  = this.data.isSelect;
    this.setData({
      choose:false,
      choosevalue:0,
      up:'/pic/三角上黑.png',
      down:'/pic/三角下黑.png',
      isSelect:!value,
      select:true
    })
  },
  /*选择种类 */
  choosetype(e){
    var index = e.currentTarget.dataset.index;

    console.log(index)
    var that = this;
    var standard = ""
    if(index == 1){
      standard = "showSelectByNew";
    }else if(index == 2){
      standard ="showSelectByHot";
    }else{
      standard ="showSelectResult";
    }
    if(that.data.searchinfo != ""){
        wx.request({
          url: 'http://127.0.0.1:8080/item/'+standard,
          method:"POST",
          data:{
            searchinfo:that.data.searchinfo
          },
          success(res){
            // console.log(res)
            var iteminfo = res.data
            for(var i=0;i<iteminfo.length;i++){
              iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
            }
            that.setData({
              itemarray:iteminfo
            })
          }
        })
    }
   
    for(var i=0 ;i<3;i++){
      var value = 'splitarray['+i+'].display';
      if(i == index){
        this.setData({
          [value]:true,
          isSelect:false,
          typec:this.data.splitarray[i].type
        })
      }
        else{
          this.setData({
            [value]:false
          })
        }
    }
    
  },
  /*选择高销量 */
  choosesale(){
 
      this.setData({
        select:false,
        choose:true,
        choosevalue:0,
        up:'/pic/三角上黑.png',
        down:'/pic/三角下黑.png',
        isSelect:false
      })
      if(this.data.searchinfo != ""){
        var that = this;
        wx.request({
          url: 'http://127.0.0.1:8080/item/showSelectBySale',
          method:"POST",
          data:{
            searchinfo:that.data.searchinfo
          },
          success(res){
            // console.log(res)
            var iteminfo = res.data
            for(var i=0;i<iteminfo.length;i++){
              iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
            }
            that.setData({
              itemarray:iteminfo
            })
          }
        })
      }

  },
  /*价格升序降序 */
  choosevalue(){
    var that = this;
    var value = this.data.choosevalue
    if(value == 0){
      // 最高
      // console.log(value)
      wx.request({
        url: 'http://127.0.0.1:8080/item/showSelectByTop',
        method:"POST",
        data:{
          searchinfo:that.data.searchinfo
        },
        success(res){
          // console.log(res)
          var iteminfo = res.data
          for(var i=0;i<iteminfo.length;i++){
            iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
          }
          that.setData({
            itemarray:iteminfo
          })
        }
      })
      this.setData({
        up:'/pic/三角上.png',
        choosevalue:1
      })
    }else if(value == 1){

      wx.request({
        url: 'http://127.0.0.1:8080/item/showSelectByLow',
        method:"POST",
        data:{
          searchinfo:that.data.searchinfo
        },
        success(res){
          // console.log(res)
          var iteminfo = res.data
          for(var i=0;i<iteminfo.length;i++){
            iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
          }
          that.setData({
            itemarray:iteminfo
          })
        }
      })
      // console.log(value)
      this.setData({
        down:'/pic/三角下.png',
        up:'/pic/三角上黑.png',
        choosevalue:2
      })
    }else{
      // console.log(value)
      wx.request({
        url: 'http://127.0.0.1:8080/item/showSelectByTop',
        method:"POST",
        data:{
          searchinfo:that.data.searchinfo
        },
        success(res){
          // console.log(res)
          var iteminfo = res.data
          for(var i=0;i<iteminfo.length;i++){
            iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
          }
          that.setData({
            itemarray:iteminfo
          })
        }
      })
      this.setData({
        up:'/pic/三角上.png',
        down:'/pic/三角下黑.png',
        choosevalue:1
      })
    }
    var value  = this.data.isSelect;
    this.setData({
      choose:false,
      select:false,
      isSelect:false
    })
    
  },
  /*选择物品 */
  chooseitem(e){
    // console.log(e)
    var index = e.currentTarget.dataset.index;
    var itemid = this.data.itemarray[index].itemID;
    wx.navigateTo({
      url: '/pages/iteminfo/iteminfo?itemid='+itemid,
    })
  },
  //展示发布者信息
  showperson(e){
    var index = e.currentTarget.dataset.index;
    var iteminfo = this.data.itemarray[index];
    this.setData({
      wxAva:iteminfo.wxAva,
      userName:iteminfo.userName,
      academy:iteminfo.academy,
      userClass:iteminfo.userClass,
      contactPhone:iteminfo.contactPhone,
      contactWx:iteminfo.contactWx,
      contactQq:iteminfo.contactQq,
      personaltarget:false
    })
  },
  cancelperson(){
    this.setData({
      personaltarget:'none'
    })
  }

  
})